
'use strict';
// 主账号：100014601307
        // 745263349@qq.com
        //密码：SEAHWOLh:f`)!pvEj!3vcKQn/fvrJ.w@
const app = require('tcb-admin-node');
const tcb_config = {env: 'food-rank-1g9s1aj2cdbd6611'};
app.init(tcb_config).auth();
var db = app.database();
exports.main = async (event, context, callback) => {
  console.log("Hello World");
  console.log(event);
  // return event;
  // return await db.collection("wealth").get();
  let { action } = event;
  if(action == "money") {
    let data = await db.collection("wealth").orderBy("money","desc").limit(200).get();
    return data;
  }else if(action == 'upRank'){
    let myRank = await db.collection('wealth').where({ name: event.params.name }).get();
    if(myRank.data.length > 0) {
      //更新数据
      await db.collection("wealth").where({ name: event.params.name }).update(event.params);
      return await db.collection("wealth").orderBy("money","desc").get();
    }else {
      await db.collection("wealth").add(event.params);
      return await db.collection("wealth").orderBy("money","desc").get();
    }
  }else if( action == 'register') {
    return await db.collection("wealth").where(event.params).get();
  }
};
